﻿using BCCommon;
using BCCommon.Enums;
using BCCommon.OAEnums;
using BCDto.Common;
using BCDto.OA.Common;
using BCDto.OA.TeamWorks.TaskRecord;
using BCDto.OA.TeamWorks.TaskRejectRecord;
using BCService.OA.TeamWorks.TaskRecordService;
using BlackCoffeeRESTful.Configs;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

namespace BlackCoffeeRESTful.Controllers.Management.OA.Workflow.TaskRecord
{
    /// <summary>
    /// OA工作流任务记录
    /// </summary>
    [ApiExplorerSettings(GroupName = "OAAdmin")]
    [Route("api/v1/sim/[controller]")]
    public class TaskRecordController : ManagementBaseController
    {
        private readonly ITaskRecordService taskRecordService;

        public TaskRecordController(ITaskRecordService taskRecordService)
        {
            this.taskRecordService = taskRecordService;
        }


        #region 获取任务记录（分页）
        /// <summary>
        /// 获取任务记录（分页）
        /// </summary>
        /// <param name="adminId">管理员Id</param>
        /// <param name="handleStatus">处理状态</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="start">开始日期</param>
        /// <param name="end">结束日期</param>
        /// <param name="ascending">排序</param>
        /// <param name="pageIndex">以 1 起始的页码 (默认 1)。</param>
        /// <param name="pageSize">每页要显示的行数 (默认 10)。</param>
        /// <response code="200">带有分页信息的日报列表。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<TaskRecordDto>), 200)]
        public IActionResult List([FromQuery(Name = "admin_id")]long? adminId,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "handle_status")]OATaskHandleStatus? handleStatus,
            [FromQuery(Name = "sort_name")]string sortName,
            [FromQuery(Name = "ascending")]bool? ascending,
            int pageIndex = 1,
            int pageSize = 10)
        {
            if (this.Admin.IsSystemAccount)
            {
                if (adminId.HasValue)
                {
                    return this.Ok(this.taskRecordService.Query(adminId.Value, start, end, handleStatus, sortName, null, ascending, pageIndex, pageSize));
                }
                return this.Ok(this.taskRecordService.Query(null, start, end, handleStatus, sortName, null, ascending, pageIndex, pageSize));
            }
            return this.Ok(this.taskRecordService.Query(this.Admin.AdminId, start, end, handleStatus, sortName, this.Admin.CompanyId, ascending, pageIndex, pageSize));
        }
        #endregion

        #region 获取我的申请任务记录列表
        /// <summary>
        /// 获取我的申请任务记录（分页）
        /// </summary>
        /// <param name="pageIndex">以 1 起始的页码 (默认 1)。</param>
        /// <param name="pageSize">每页要显示的行数 (默认 10)。</param>
        /// <response code="200">带有分页信息的日报列表。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<TaskRecordDto>), 200)]
        public IActionResult MyApplied(int pageIndex = 1,
            int pageSize = 10)
        {
            if (this.Admin.IsSystemAccount)
            {
                return this.Ok(this.taskRecordService.Query(null, null, null, null, null, this.Admin.CompanyId, null, pageIndex, pageSize, 0));
            }
            return this.Ok(this.taskRecordService.Query(this.Admin.AdminId, null, null, null, null, this.Admin.CompanyId, null, pageIndex, pageSize, 0));
        }
        #endregion

        #region 获取我审批的任务记录列表
        /// <summary>
        /// 获取我审批的任务记录（分页）
        /// </summary>
        /// <param name="pageIndex">以 1 起始的页码 (默认 1)。</param>
        /// <param name="pageSize">每页要显示的行数 (默认 10)。</param>
        /// <response code="200">带有分页信息的日报列表。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<TaskRecordDto>), 200)]
        public IActionResult MyApproved(int pageIndex = 1,
            int pageSize = 10)
        {
            if (this.Admin.IsSystemAccount)
            {
                return this.Ok(this.taskRecordService.Query(null, null, null, null, null, this.Admin.CompanyId, null, pageIndex, pageSize, null, true));
            }
            return this.Ok(this.taskRecordService.Query(this.Admin.AdminId, null, null, null, null, this.Admin.CompanyId, null, pageIndex, pageSize, null, true));
        }
        #endregion

        #region 获取活动实例驳回记录列表
        /// <summary>
        /// 获取驳回记录列表
        /// </summary>
        /// <response code="200">获取驳回记录列表。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<TaskRejectRecordViewDto>), 200)]
        public IActionResult RejectRecords([FromQuery(Name = "admin_id")]long? adminId,
            [FromQuery(Name = "start")]DateTime? start,
            [FromQuery(Name = "end")]DateTime? end,
            [FromQuery(Name = "sort_name")]string sortName,
            [FromQuery(Name = "ascending")]bool? ascending,
            int pageIndex = 1,
            int pageSize = 10)
        {
            if (this.Admin.IsSystemAccount)
            {
                if (adminId.HasValue)
                {
                    return this.Ok(this.taskRecordService.GetRejectRecords(adminId.Value, start, end, sortName, this.Admin.CompanyId, ascending, pageIndex, pageSize));
                }
                return this.Ok(this.taskRecordService.GetRejectRecords(null, start, end, sortName, this.Admin.CompanyId, ascending, pageIndex, pageSize));
            }
            return this.Ok(this.taskRecordService.GetRejectRecords(this.Admin.AdminId, start, end, sortName, this.Admin.CompanyId, ascending, pageIndex, pageSize));
        }

        #endregion

        #region 查询任务记录详情
        /// <summary>
        /// 获取任务记录详情信息
        /// </summary>
        /// <param name="taskRecordNo">任务单编号</param>
        /// <returns></returns>
        [HttpGet("{task_record_no}")]
        [ProducesResponseType(typeof(TaskRecordViewDto), 200)]
        public IActionResult Get([FromRoute(Name = "task_record_no")] string taskRecordNo)
        {
            return this.Ok(this.taskRecordService.GetDetailEntity(taskRecordNo));
        }

        #endregion

        #region 查询保证金任务记录详情
        /// <summary>
        /// 获取保证金任务记录详情信息
        /// </summary>
        /// <param name="vehicleDepositModifyApplyRecordCode">保证金修改申请码</param>
        /// <returns></returns>
        [HttpGet("[action]/{vehicle_deposit_modify_apply_record_code}")]
        [ProducesResponseType(typeof(TaskRecordViewDto), 200)]
        public IActionResult GetVehicleDepositModify([FromRoute(Name = "vehicle_deposit_modify_apply_record_code")] string vehicleDepositModifyApplyRecordCode)
        {
            return this.Ok(this.taskRecordService.GetTaskRecordInfo(vehicleDepositModifyApplyRecordCode));
        }

        #endregion

        /// <summary>
        ///  处理有被驳回的任务为完成
        /// </summary>
        /// <param name="taskRecordNo">任务单编号</param>
        /// <response code="200">带有分页信息的日报列表。</response>
        /// <returns></returns>
        [HttpPost("[action]/{task_record_no}")]
        [Log("处理有被驳回的任务为完成")]
        [ProducesResponseType(typeof(bool), 200)]
        public IActionResult HandleHasRejectedRecord([FromRoute(Name = "task_record_no")]string taskRecordNo)
        {
            return this.Ok(this.taskRecordService.HandleHasRejectedRecord(taskRecordNo));
        }

        #region 获取今日待处理任务

        /// <summary>
        /// 获取今日待处理任务
        /// </summary>
        /// <response code="200">获取今日待处理任务。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<TaskRecordGroupWorkflowTypeDto>), 200)]
        public IActionResult PendingTaskList()
        {
            return this.Ok(this.taskRecordService.GetPendingTaskList(this.Admin.AdminId, RoleType.Admin));
        }
        #endregion

        #region 获取我发起的任务列表

        /// <summary>
        /// 获取我发起的任务列表
        /// </summary>
        /// <response code="200">获取我发起的任务列表。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<TaskRecordGroupWorkflowTypeDto>), 200)]
        public IActionResult AppliedTaskList()
        {
            return this.Ok(this.taskRecordService.GetAppliedTaskList(this.Admin.AdminId, RoleType.Admin));
        }
        #endregion

        #region 我的任务和进度

        /// <summary>
        /// 我的任务和进度
        /// </summary> 
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="status">处理状态</param>
        /// <param name="sortName">排序字段</param> 
        /// <param name="ascending">是否升序排序</param>
        /// <param name="pageIndex">分页页码</param>
        /// <param name="pageSize">分页页数</param>
        /// <returns>我的任务进度</returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<TaskRecordProgressDto>), 200)]
        public IActionResult TaskProgressList([FromQuery(Name = "start_date")]DateTime? startDate,
            [FromQuery(Name = "end_date")]DateTime? endDate,
            [FromQuery(Name = "status")]OATaskHandleStatus? status,
            [FromQuery(Name = "sort_name")]string sortName,
            [FromQuery(Name = "ascending")]bool? ascending,
            int pageIndex = 1,
            int pageSize = 10)
        {
            return this.Ok(this.taskRecordService.GetTaskProgressList(this.Admin.AdminId, RoleType.Admin, startDate, endDate, status, sortName, ascending, pageIndex, pageSize));
        }
        #endregion

        /// <summary>
        /// 关闭指定的工作流任务。
        /// </summary>
        /// <param name="taskRecordNo">任务记录编号 。</param>
        /// <param name="requestDto"></param>
        /// <response code="200">关闭指定的工作流任务。</response>
        /// <returns></returns>
        [HttpPut("[action]/{task_record_no}")]
        [ProducesResponseType(typeof(bool), 200)]
        public IActionResult CloseWorkflowTask([FromRoute(Name = "task_record_no")]string taskRecordNo, [FromBody] RemarkRequestDto requestDto)
        {
            return this.Ok(this.taskRecordService.UpdateCloseWorkflowTask(taskRecordNo, requestDto));
        }
    }
}